"""Constraints corresponding to product variables."""

load(":constants.bzl", "constants")
load(":settings.bzl", "soong_config_variables")

package(default_visibility = ["//visibility:public"])

# Unlike product config variables below, these are dynamically generated from
# Soong, since the list of config variables are dynamically defined in
# Android.bp files and not hardcoded into Soong.
soong_config_variables(
    bool_vars = constants.SoongConfigBoolVariables,
    value_vars = constants.SoongConfigValueVariables,
    string_vars = constants.SoongConfigStringVariables,
)

# Generate one constraint_value for each product_variable
# The constraint_value for <var> can be within a select() to specify an
# attribute value for the same conditions product_variable.<var>, for most
# cases, that is when the value of <var> is true. For example,
#
# product_variables: {
#     debuggable: {
#         cflags: ["debug_flag1", "debug_flag2"],
#     },
# }
#
# translates into:
#
# cflags = select({
#   "//build/bazel/product_variables:debuggable": ["debug_flag1", "debug_flag2"],
#   "//conditions:default": [],
# }),
[
    (constraint_setting(name = product_variable+"_constraint"),
   constraint_value(name = product_variable, constraint_setting = product_variable+"_constraint",)
   )
 for product_variable in constants.ProductVariables.keys()
]

# Caution: do not use these arch-variant product variables directly.
# If you have a complex combination of product variable and architecture/os/etc,
# prefer instead to craft an appropriate configuration in your BUILD file.
# See: https://docs.bazel.build/versions/master/configurable-attributes.html
# Within bp2build, :safestack-android should be used when an attribute value is
# conditional on both safestack:true and the os is android.
#
# e.g.
# target: {
#     android: {
#         product_variables: {
#             safestack: {
#                 cflags: ["-Dsafestack-android"],
#             },
#         },
#     },
# },
#
# would translate to:
#
# cflags = select({
#     "//build/bazel/product_variables:safestack-android": ["-Dsafestack-android"],
#     "//conditions:default": [],
# }),
[
    [config_setting(
        name = product_variable + "-" + variant,
        constraint_values = [
            ":" + product_variable,
            variantConstraint,
        ],
    ) for variant, variantConstraint in constants.ArchVariantToConstraints.items()]
    for product_variable in constants.ArchVariantProductVariables
]
